package controller.frontOffice;

import java.io.Serializable;
import java.util.Map;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;

import org.primefaces.context.RequestContext;

import ejb.order.OrderService;
import ejb.paiement.PaiementService;
import entities.Order;
import entities.Paiement;

@Named
@RequestScoped
public class PaiementController implements Serializable {

	private static final long serialVersionUID = 1L;

	@EJB
	private PaiementService paiementService;

	@EJB
	private OrderService orderService;
	
	private Paiement paiement;

	public String pagePaiement(){
		Order o = new Order();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String orderId1 = (String)requestMap.get("orderId");  
		Long orderId = Long.parseLong(orderId1);
		o = orderService.find(orderId);
		
		FacesContext ctx = FacesContext.getCurrentInstance();
		OrderController oo = ctx.getApplication().evaluateExpressionGet(ctx, "#{orderController}", OrderController.class);
		oo.setTempOrder(o);
		
		return "indexPaiement";
	}

	public String createPaiement() {
		Order o = new Order();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String orderId1 = (String)requestMap.get("orderId");  
		Long orderId = Long.parseLong(orderId1);
		o = orderService.find(orderId);
		o.setStatutPaiement(1);
		
		orderService.update(o);
		
		paiement.setOrder(o);
		paiementService.create(paiement);
		
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Votre paiement est bien soumis",  "Votre paiement est bien soumis");
		
		FacesContext.getCurrentInstance().addMessage(null, message);

		return "home";
	}

	public PaiementService getPaiementService() {
		return paiementService;
	}



	public void setPaiementService(PaiementService paiementService) {
		this.paiementService = paiementService;
	}



	public Paiement getPaiement() {
		if (paiement == null) {
			paiement = new Paiement();
		}
		return paiement;
	}



	public void setPaiement(Paiement paiement) {
		this.paiement = paiement;
	}


}
